<?php
namespace app\Admin\controller\user;

//引入公共控制器
use app\common\controller\Backend;


class User extends Backend
{
     
    
    public function __construct(){

        //继承父类
        parent::__construct();

        $this->UserModel = model('User.User');
        $this->RecordModel = model('User.Record');
    
    }   
    
    // 会员列表
    public function index()
    {
        $count = $this->UserModel->count();

        $countlist = $this->UserModel->order('point', 'desc')->paginate(10);


        $pagestr = $countlist->render();

        // var_dump($countlist);
        // exit;

        $this->assign([
            'count' => $count,
            'countlist' => $countlist,
            'pagestr' => $pagestr
        ]);

        return $this->fetch();
    }

    public function del()
    {
        if($this->request->isPost()){

            $success = [
                'result' => false,
                'msg' => ''
            ];

            $ids = $this->request->param('ids', 0);

            $result = $this->UserModel->destroy($ids);


            if($result === FALSE)
            {
                $success['result'] = false;
                $success['msg'] = $this->UserModel->getError();
            }else
            {
                $success['result'] = true;
                $success['msg'] = '删除配置成功';
            }

            return json($success);
        }
    }



    public function edit(){


        $id = $this->request->param('id', 0);

        $userinfo = $this->UserModel->find($id);

        $this->assign('info',$userinfo);

        // var_dump($userinfo);
        // exit;

        if($this->request->isPost()){
            $param = $this->request->param();
            $rspss = $this->request->param('rspss','');
            
            $data = [
                'nickname' => $param['nickname'],
                'sex'=>$param['sex']
            ];


            if($rspss){
                if(md5($param['rspss'].$userinfo['salt']) != $userinfo['password']){
                    $this->error('密码错误');
                    exit;
                }
                if($param['password'] == $param['rspss']){
                    $this->error('更改的密码不能与原密码相同');
                    exit;
                }
                $data['password'] = md5($param['password'].$userinfo['salt']);
            }


            $result = $this->UserModel->where('id', $id)->update($data);
            if(!$result){
                $this->error('编辑失败');
                exit;
            }else{
                $this->success('编辑成功','/admin/user.user/index');
                exit;
            }
        }

        


        return $this->fetch();
    }

}